package week12.chat;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;

/**
 * @author The setting sun
 * @version 1.0
 * @title: JDBCOperation
 * @description: TODO
 * @date 2021/11/25 9:01
 */
public class JDBCOperation {
    public static void main(String[] args) {
//        Connection connection = getConnection();
//        System.out.println(connection);
    }

    /**
     * @author: The setting sun
     * @param: []
     * @return: java.sql.Connection
     * @description: 获取数据库连接
     * @date: 2021/11/25 14:18
     */
    public static Connection getConnection() {
        try {
            String url = "jdbc:mysql://localhost:3306/account?useUnicode=true&characterEncoding=utf8";
            String user = "root";
            String password = "1509982360";

            Connection connection = DriverManager.getConnection(url, user, password);

            return connection;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    /**
     * @author: The setting sun
     * @param: [connection, sql, urgs]
     * @return: void
     * @description: 添加用户
     * @date: 2021/11/25 14:18
     */
    public static void addUser(Connection connection, String sql, Object... urgs) {
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < urgs.length; i++) {
                ps.setObject(i + 1, urgs[i]);
            }

            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * @author: The setting sun
     * @param: [connection, sql, urgs]
     * @return: java.util.ArrayList<week12.chat.Account>
     * @description: 匹配账号和密码
     * @date: 2021/11/25 14:18
     */
    public static ArrayList<Account> verificationUser(Connection connection, String sql, Object... urgs) {
        ArrayList<Account> list = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            list = new ArrayList<>();
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < urgs.length; i++) {
                ps.setObject(i + 1, urgs[i]);
            }

            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();

            while (rs.next()) {
                Account account = new Account();

                for (int i = 0; i < rsmd.getColumnCount(); i++) {
                    Object value = rs.getObject(i + 1);

                    String columnLabel = rsmd.getColumnLabel(i + 1);
                    Field field = Account.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(account, value);
                }

                list.add(account);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * @author: The setting sun
     * @param: [connection, ps, rs]
     * @return: void
     * @description: 关闭资源
     * @date: 2021/11/25 14:18
     */
    public static void closeResource(Connection connection, PreparedStatement ps, ResultSet rs) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
